home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS04.ADF / C / timrstuf.c < prev    next >
C/C++ Source or Header  |  1985-10-30  |  5KB  |  148 lines

  1. /* timerstuff.c v1.0 */
  2.  
  3. /****************************************************************
  4. *                                                               *
  5. * Copyright 1985, Commodore Amiga Inc.  All rights reserved.    *
  6. * No part of this program may be reproduced, transmitted,       *
  7. * transcribed, stored in retrieval system, or translated into   *
  8. * any language or computer language, in any form or by any      *
  9. * means, electronic, mechanical, magnetic, optical, chemical,   *
  10. * manual or otherwise, without the prior written permission of  *
  11. * Commodore Amiga Incorporated, 983 University Ave, #D          *
  12. * Los Gatos, CA 95030                                           *
  13. *                                                               *
  14. ****************************************************************/
  15. /* ******************************************************************** */ 
  16. /* Exec Support Functions:  CreateTimer, WaitTimer, DeleteTimer         */
  17. /*
  18. /* CreateTimer() returns a pointer to an IOStdReq structure, set up
  19.  * for communications with the timer device.  Returns NULL if it
  20.  * cannot allocate the timer or get enough memory or signals.
  21.  * 
  22.  * WaitTimer(IOStdReq,seconds,microseconds) uses the communications
  23.  * block from CreateTimer().  It puts your task to sleep until the
  24.  * timer counts down at least this amount of time.  
  25.  *
  26.  * SetTimer(IOStdReq,seconds,microseconds) returns a pointer to a port
  27.  * (copied from the IOStdReq) to which the timer returns the message
  28.  * block when the timeout has completed.  It uses SendIO to transmit
  29.  * the IORequest.  This means your task can go on to something else,
  30.  * then execute WaitPort(timerReplyPort), going to sleep until the
  31.  * timer has timed out.  You will still have to do a GetMsg(timerReplyPort)
  32.  * after your task awakens, where with WaitTimer this is not necessary.
  33.  *
  34.  * DeleteTimer(IOStdReq) uses the block from CreateTimer() to free up
  35.  * memory and signal bits that have been allocated for this purpose.
  36.  *
  37.  * syntax Summary:  struct IOStdReq *CreateTimer();
  38.  *                  int WaitTimer();
  39.  *                  struct Port *SetTimer();
  40.  *                  int DeleteTimer();
  41.  *
  42.  * 
  43.  *      typical program call:
  44.  *      
  45.  *              struct IOStdReq *timerA;
  46.  *              struct Port *timerAPort;
  47.  *              ...
  48.  *              timerA = CreateTimer();
  49.  *              if (timerA == NULL) exit(ERROR_IN_CREATETIMER);
  50.  *              timerAPort = timerA->io_Message.mn_ReplyPort;
  51.  *              ...
  52.  *              WaitTimer(timerA,3,100000);  wait at least 3.1 seconds 
  53.  *              ...
  54.  *              SetTimer(timerA,2,0);    tell timer to start
  55.  *              while( DOING_SOMETHING )
  56.  *                      {
  57.  *                      do_it();        
  58.  *                      if(CheckPort(timerAPort)) 
  59.  *                         {  GetMsg(timerAPort);    retrieve your msg
  60.  *                            do_something_else();   skipped unless timed out
  61.  *                            SetTimer(timerA,2,0);  set timer again. 
  62.  *                         }
  63.  *
  64.  * Author: Rob Peck 10/11/85 
  65.  */
  66.  
  67.  
  68. #include <exec/types.h>
  69. #include <exec/lists.h>
  70. #include <exec/nodes.h>
  71. #include <exec/ports.h>
  72. #include <exec/io.h>
  73. #include <exec/devices.h>
  74. #include <devices/timer.h>
  75.  
  76. #define SECONDS io_Actual
  77. #define MICROSECONDS io_Length
  78. /* redefine fields in IOStdReq so as to match requirements of a timeval */
  79.  
  80.  
  81. extern struct Port *CreatePort();
  82. extern struct IOStdReq *CreateStdIO();
  83.  
  84. struct IOStdReq 
  85. *CreateTimer()          /* return a pointer to an IOStdReq if 
  86.                          * it was possible to allocate a new
  87.                          * timer */
  88. {
  89.         SHORT error;
  90.         struct Port *timerport;
  91.         struct IOStdReq *timermsg;
  92.         timerport = CreatePort(0,0);
  93.         if (timerport == NULL)
  94.                 return(NULL);  /* Error during CreatePort */
  95.         timermsg = CreateStdIO(timerport);
  96.         if (timermsg == NULL)
  97.                 {
  98.                 DeletePort(timerport);
  99.                 return(NULL);  /* Error during CreateStdIO */
  100.                 }
  101.         error = OpenDevice(TIMERNAME, UNIT_MICROHZ, timermsg, 0);
  102.         if (error != 0)
  103.                 {
  104.                 DeleteStdIO(timermsg);
  105.                 DeletePort(timerport);
  106.                 return(NULL);  /* Error during OpenDevice */
  107.                 }
  108.         return(timermsg);
  109. }
  110.  
  111. struct Port 
  112. *SetTimer(whichtimer,seconds,microseconds)
  113. ULONG seconds,microseconds;
  114. struct IOStdReq *whichtimer;
  115. {
  116.         struct Port *tempPort;
  117.         tempPort = whichtimer->io_Message.mn_ReplyPort;
  118.         whichtimer->io_Command = TR_ADDREQUEST;   /* add a new timer request */
  119.         whichtimer->SECONDS = seconds;            /* seconds */
  120.         whichtimer->MICROSECONDS = microseconds;  /* microseconds */
  121.         SendIO(whichtimer);
  122.         return(tempPort);
  123. }
  124.  
  125. int
  126. WaitTimer(whichtimer,seconds,microseconds)
  127. ULONG seconds,microseconds;
  128. struct IOStdReq *whichtimer;
  129. {
  130.         whichtimer->io_Command = TR_ADDREQUEST;   /* add a new timer request */
  131.         whichtimer->SECONDS = seconds;            /* seconds */
  132.         whichtimer->MICROSECONDS = microseconds;  /* microseconds */
  133.         DoIO(whichtimer);
  134.         return(0);
  135. }
  136.  
  137. int
  138. DeleteTimer(whichtimer)
  139. struct IOStdReq *whichtimer;
  140. {
  141.         struct Port *whichport;
  142.         whichport = whichtimer->io_Message.mn_ReplyPort;
  143.         DeleteStdIO(whichtimer);
  144.         DeletePort(whichport);
  145.         return(0);      
  146. }
  147.  
  148.